//@flow

/**
 * app版本控制
 * 1，app store版本
 * 2，code push版本
 * 
 * 更新检查步骤：
 * 1，检查app store是否有新版本
 *      -有：弹出通知，展示新内容 根据是否为强制更新 确定显示按钮 “暂不更新”“立即更新”
 *      -无：检查code push 更新
 * 2，code push更新
 *      -正在检查更新
 *      -暂无更新
 *      -发现有新内容
 *      -正在下载新版本
 *      -新版本下载完毕
 *      -
 */

import React, { Component } from 'react';
import { 
    ScrollView,
    Text,
    Platform,
    View,
 } from 'react-native';
import { APP_ID_APP_STORE, APP_DOWNLOAD_URL_FOR_ANDROID } from '../config/config';
import codePush from "react-native-code-push";
import { Button } from 'antd-mobile-rn';
import { Navigate } from '../common/navigate';
import { NavBar } from './navBar';
/**app 更新状态 */
type AppStatusAPPStoreChecking = {type:0,label:'app store版本检查中'}
type AppStatusAPPStoreHasUpdate= {type:1,label:'app store发现新版本'}
type AppStatusAPPStoreNoUpdate = {type:2,label:'app store未发现新版本'}
type AppStatusCodepushChecking = {type:3,label:'code push版本检查中'}
type AppStatusCodepushHasUpdate= {type:4,label:'code push发现新版本'}
type AppStatusCodepushNoUpdate = {type:5,label:'code push未发现新版本'}
type AppStatusCodepushDownloading={type:6,label:'code push包正在下载'}
type AppStatusCodepushDownloaded={type:7,label:'code push包下载完毕'}
type AppStatusCodepushInstalling={type:8,label:'code push包正在安装'}
type AppStatusCodepushInstalled ={type:9,label:'code push包安装完毕'}
export type AppStatus = AppStatusAPPStoreChecking
                        |AppStatusAPPStoreHasUpdate
                        |AppStatusAPPStoreNoUpdate
                        |AppStatusCodepushChecking
                        |AppStatusCodepushHasUpdate
                        |AppStatusCodepushNoUpdate
                        |AppStatusCodepushDownloading
                        |AppStatusCodepushDownloaded
                        |AppStatusCodepushInstalling
                        |AppStatusCodepushInstalled

/**app store版本检查结果 */
type AppStoreCheckResult = (hasUpdate:boolean)=>{}

export type Props = {};
export type State = {
    appStatus   :AppStatus,
};


class VersionControl extends Component <Props,State>{
    /**生命周期函数 */
    componentDidMount(){

    }
    /**初始化state */
    state:State = {
        appStatus : {type:0,label:'app store版本检查中'}
    }
    /**app状态变化回调 */
    codePushStatusDidChange(status) {
        switch(status) {
            case codePush.SyncStatus.CHECKING_FOR_UPDATE:
                console.log("Checking for updates.");
                break;
            case codePush.SyncStatus.DOWNLOADING_PACKAGE:
                console.log("Downloading package.");
                break;
            case codePush.SyncStatus.INSTALLING_UPDATE:
                console.log("Installing update.");
                break;
            case codePush.SyncStatus.UP_TO_DATE:
                console.log("Up-to-date.");
                break;
            case codePush.SyncStatus.UPDATE_INSTALLED:
                console.log("Update installed.");
                break;
        }
    }
    /**code push下载进度更新 */
    codePushDownloadDidProgress(progress:number) {
        console.log(progress.receivedBytes + " of " + progress.totalBytes + " received.");
    }
    /**检查app store版本
     * 这里app store不仅仅包括 iOS的应用商店也包括android的应用商店
     * 系统自动判断在哪个平台进行更新
     */
    appStoreChecking(appId:string=APP_ID_APP_STORE,androidDownloadUrl:string=APP_DOWNLOAD_URL_FOR_ANDROID):Promise<AppStoreCheckResult>{
        if(Platform.OS === 'ios'){

        }else if(Platform.OS == 'android'){

        }else{
            return new Promise((resolve,reject)=>{
                return resolve(false)
            })
        }
    }
    /**检查code push版本 */
    codePushChecking(){
        codePush.sync()
    }
    /**生命周期函数 */
    componentDidMount(){
        this.codePushChecking()
    }
    /**渲染函数 */
    render(){
        return (
            <View>
                {/* <NavBar titleView="更新"/> */}
                <Text>更新组件</Text>
                <Button onClick={()=>{Navigate.navigate('Root')}}>打开首页</Button>
            </View>
        )
    }
}

VersionControl = codePush({checkFrequency:codePush.CheckFrequency.MANUAL,
                           deploymentKey:'',
                           installMode:codePush.InstallMode.IMMEDIATE,
                           mandatoryInstallMode:codePush.InstallMode.IMMEDIATE,
                           updateDialog:{
                               appendReleaseDescription:true,
                               descriptionPrefix:'',
                               mandatoryContinueButtonLabel:'',
                               mandatoryUpdateMessage:'',
                               optionalIgnoreButtonLabel:'',
                               optionalInstallButtonLabel:'',
                               optionalUpdateMessage:'',
                               title:''
                           }})(VersionControl)
                        

export {VersionControl};